//
//  Project.m
//  addActionForm
//
//  Created by user on 11/05/26.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "Project.h"


@implementation Project

@synthesize projects;
@synthesize actionList;
@synthesize problemList;
@synthesize commentList;
@synthesize resourceList;
@synthesize projectState;
@synthesize historyList;
@synthesize riskList;
@synthesize isNew;
@synthesize changed;

- (id) initForNew{
	self = [super init];	
	actionList = [[NSMutableArray alloc] init];
	problemList = [[NSMutableArray alloc] init];
	commentList = [[NSMutableArray alloc] init];
	resourceList = [[NSMutableArray alloc] init];
	projects = [[Projects alloc] init];
	projects.vName = @"New Project";
	projects.vDescription = @"Brief description of the project here...";
	projects.vStart_DateTime = [Library getCurrentTime];
	projects.vFinish_Datetime = [Library getCurrentTime];
	isNew = YES;
	[self loadStatus];
	return self;
}

- (id) initWithProjects : (Projects *) pprojects {
	self = [super init];
	projects = pprojects;
	actionList = [[NSMutableArray alloc] init];
	problemList = [[NSMutableArray alloc] init];
	commentList = [[NSMutableArray alloc] init];
	resourceList = [[NSMutableArray alloc] init];
    riskList = [[NSMutableArray alloc] init];
	[self loadStatus];
	return self;
}

- (NSString *) getProjectName{
	return [projects vName];
}

- (NSString *) getProjectDescription{
	return [projects vDescription];
}

- (NSString *) getProjectStatus{
	return [projectState getDescription];
}

- (double) getProjectStartTime{
	return [projects vStart_DateTime];
}

- (double) getProjectFinishTime{
	return [projects vFinish_Datetime];
}

- (double) getProjectStatusTime{
	//O tipo de dados esta como string na bd e devia estar como datetime.
	return [projects vStatus_DateTime];
}


- (void) setProjectStartTime : (double) dateTime{
	changed = YES;
	[projects setVStart_DateTime:dateTime];
}

- (void) setProjectFinishTime : (double) dateTime{
	changed = YES;
	[projects setVFinish_Datetime:dateTime];
}

- (void) setProjectStatusTime : (double) dateTime{
	changed = YES;
	[projects setVStatus_DateTime:dateTime];
	return;
}

- (SEL) getSetProjectStartTimeSelector{
	return @selector (setProjectStartTime:);
}

- (SEL) getSetProjectFinishTimeSelector{
	return @selector (setProjectFinishTime:);
}

- (SEL) getSetProjectStatusTimeSelector{
	return @selector (setProjectStatusTime:);
}

- (int) getProjectId{
	return [projects vId_Projects];
}

- (NSString *) getInsertString{
	return [projects getInsertStringForSelf];
}

- (NSString *) getUpdateString{
	return [projects getUpdateStringForSelf];
}

- (void) setProjectName : (NSString *) projectName{
	changed = YES;
	[projects setVName:projectName];
}

- (void) setProjectDescription : (NSString *) projectDescription{
	changed = YES;
	[projects setVDescription:projectDescription];
}

- (void) insertNewAction : (Action *) action{
	[actionList addObject:action];
}

- (void) insertNewProblem : (Problem *) problem{
	[problemList addObject:problem];
}

- (void) insertNewComment : (Comment *) comment{
	[commentList addObject:comment];
}

- (void) loadStatus {
	DataContext *context = [[DataContext alloc]init];
	NSMutableArray * colStates = [[NSMutableArray alloc] init];
	[context getFromQueryToArrayStates: [projects getSelectStringForStates] : colStates];
	[context dealloc];
	if([colStates count]>0){
		projectState = [[State alloc] initWithStates:[colStates objectAtIndex:0]];
		[colStates removeAllObjects];
	}else {
		projectState = nil;
	}
	[colStates dealloc];
}

- (void) loadActions{
	DataContext *context = [[DataContext alloc]init];
	[context getFromQueryToArrayActions : [projects getSelectStringForActions] : [projects colActions]];
	[context dealloc];
	for(Actions *a in [projects colActions])
	{
		Action *action = [[Action alloc] initWithActions:a];
		[actionList addObject:action];
	}
}

- (void) loadStates{
	DataContext *context = [[DataContext alloc]init];
	[context getFromQueryToArrayStates:[projects getSelectStringForStates] : [projects colStates]];
	[context dealloc];
}

-(void) loadProblems{
	DataContext *context = [[DataContext alloc]init];
	[context getFromQueryToArrayProblems: [projects getSelectStringForProblems] : [projects colProblems]];
	[context dealloc];
	for(Problems *p in [projects colProblems])
	{
		Problem *problem = [[Problem alloc] initWithProblems:p];
		[problemList addObject:problem];
	}
	
}

- (void) loadComments{
	DataContext *context = [[DataContext alloc]init];
	[context getFromQueryToArrayComments:[projects getSelectStringForComments] : [projects colComments]];
	[context dealloc];
	for(Comments *p in [projects colComments])
	{
		Comment *comment = [[Comment alloc] initWithComments:p];
		[commentList addObject:comment];
	}
}

- (void) loadResources{
	DataContext *context = [[DataContext alloc]init];
	NSMutableArray *colRelation = [[NSMutableArray alloc] init]; 
	[context getFromQueryToArrayProjectResources:[projects getSelectStringForProjectResources] : colRelation];
	for(ProjectResources *projectResources in colRelation)
	{
		[context getFromQueryToArrayResources:[projectResources getSelectStringForResources] : [projectResources colResources]];
		Resources *resources = [[projectResources colResources] objectAtIndex:0];
		Resource *resource = [[Resource alloc] initWithResources:resources];		
		[[projectResources colResources] removeObjectAtIndex:0];
		[projectResources dealloc];
		[resourceList addObject:resource];
	}
	[context dealloc];
}

- (void) loadHistory{
	DataContext *context = [[DataContext alloc]init];
	[context getFromQueryToArrayHistorys:[projects getSelectStringForHistorys] : [projects colHistorys]];
	[context dealloc];
	for(Historys *hist in [projects colHistorys])
	{
		History *history = [[History alloc] initWithHistorys:hist];
		[historyList addObject:history];
	}
}

- (void) loadRisks {
    NSLog(@"Entra no loadRisks");
  	DataContext *context = [[DataContext alloc]init];
	[context getFromQueryToArrayRisks: [projects getSelectStringForRisks] : [projects colRisks]];
	[context dealloc];
	for(Risks *r in [projects colRisks])
	{
		Risk *risk = [[Risk alloc] initWithRisks:r];
        NSLog(@"ID risk: %d", [risk getID]);
		[riskList addObject:risk];
        NSLog(@"riscos cont: %d", [riskList count]);
	}	
    
    NSLog(@"n.riscos %d", [riskList count]);
}

- (void) unloadResources{
	int conta = [resourceList count];
	for(int i = 0; i < conta; i++)
	{
		Resource * resource = [resourceList objectAtIndex: 0];
		[resourceList removeObjectAtIndex:0];
		
		//Clean the array of objects
		[[projects colProjectResources] removeObjectAtIndex:0];
		
		[resource dealloc];
		resource = nil;
	}
}

- (void) unloadComments{
	int conta = [commentList count];
	for(int i = 0; i < conta; i++)
	{
		Comment * comment = [commentList objectAtIndex: 0];
		[commentList removeObjectAtIndex:0];
		
		//Clean the array of objects
		[[projects colComments] removeObjectAtIndex:0];
		
		[comment dealloc];
		comment = nil;
	}
}


- (void) unloadActions{
	int conta = [actionList count];
	for(int i = 0; i < conta; i++)
	{
		Action * action = [actionList objectAtIndex: 0];
		[actionList removeObjectAtIndex:0];
		
		//Clean the array of objects
		[[projects colActions] removeObjectAtIndex:0];		
		
		[action dealloc];
		action = nil;
	}
}

- (void) unloadProblems{
	int conta = [problemList count];
	for(int i = 0; i < conta; i++)
	{
		Problem * problem = [problemList objectAtIndex:0];
		[problemList removeObjectAtIndex:0];
		
		//Clean the array of objects
		[[projects colProblems] removeObjectAtIndex:0];
		
		[problem dealloc];
		problem = nil;
	}
}

- (void) unloadRisks{
	int conta = [riskList count];
	for(int i = 0; i < conta; i++)
	{
		Risk *risk = [riskList objectAtIndex: 0];
		[riskList removeObjectAtIndex:0];
		
		//Clean the array of objects
		[[projects colRisks] removeObjectAtIndex:0];
		
		[risk dealloc];
		risk = nil;
	}
}


- (void) dealloc{
	/*[actionList dealloc];
     [problemList dealloc];
     [commentList dealloc];
     [resourceList dealloc];
     [super dealloc];
     actionList = nil;
     problemList = nil;
     commentList = nil;
     resourceList = nil;
     projects = nil;*/
	[super dealloc];
}

@end
